package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Bizlogic.CAManager;
import DAO.DAOException;
import Model.Product;

import com.rms.util.Constant;

/**
 * @author TONY
 * 
 */
public class SearchPro extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SearchPro() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Product p = new Product();
		String num = request.getParameter("num");
		if (num.trim().equals("")) {
			p.setProductnum(0);
			p.setDescription(request.getParameter("decs"));
		} else {
			p.setProductnum(Integer.parseInt(request.getParameter("num")));
			p.setDescription(request.getParameter("decs"));
		}
		CAManager ca = new CAManager();
		try {
			List<Product> list = ca.findProductsByCriteria(p);
			request.setAttribute("list", list);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.getRequestDispatcher("MaintainInvertoryStock.jsp").forward(
				request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ua = request.getHeader(Constant.HEAD);
		PrintWriter out = response.getWriter();
		if (Constant.MIDP_USER_AGENT.equals(ua)) {
			String partnum = request.getParameter("partnum");
			String partdes = request.getParameter("partdes");
			System.out.print(partnum);
			System.out.print(partdes);
			Product p = new Product();
			CAManager ca = new CAManager();

			p.setProductnum(Integer.parseInt(partnum));
			p.setDescription(partdes);

			List<Product> list = new ArrayList<Product>();
			try {
				list = ca.findProductsByCriteria(p);
				System.out.print(list.size());
				out.println("Find    " + list.size() + "    results       ");
				for (Product product : list) {
					out.print("Product Number is     "
							+ product.getProductnum() + "Stock is     "
							+ product.getInstock());
				}

			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// EmployeeDAO ed=new EmployeeDAOImpl();

		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
